﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace DotNetX
{
    public static class ConvertExtensions
    {
        public static bool IsInt32(string s)
        {
            int r = 0;
            return Int32.TryParse(s, out r);
        }

        public static bool IsMonth(string month)
        {
            bool res = false;

            if (month == null)
                return false;

            month = month.ToLower().Trim();

            foreach (var m in CultureInfo.CurrentCulture.DateTimeFormat.AbbreviatedMonthNames)
            {
                if (month == m.Trim().ToLower())
                {
                    res = true;
                    break;
                }
            }

            if (!res)
            {
                foreach (var m in CultureInfo.CurrentCulture.DateTimeFormat.MonthNames)
                {
                    if (month == m.Trim().ToLower())
                    {
                        res = true;
                        break;
                    }
                }
            }

            return res;
        }

        public static int FromMonth(string month)
        {
            int res = 0;

            if (month == null)
                return res;

            month = month.ToLower().Trim();

            int i = 0;
            foreach (var m in CultureInfo.CurrentCulture.DateTimeFormat.AbbreviatedMonthNames)
            {
                i++;
                if (month == m.Trim().ToLower())
                {
                    res = i;
                    break;
                }
            }

            if (res == 0)
            {
                i = 0;
                foreach (var m in CultureInfo.CurrentCulture.DateTimeFormat.MonthNames)
                {
                    i++;
                    if (month == m.Trim().ToLower())
                    {
                        res = i;
                        break;
                    }
                }
            }

            if (res == 0)
                throw new FormatException(String.Format("'{0}' is an invalid month."));

            return res;
        }

        public static T ToType<T>(string value)
        {
            Type type = typeof(T);

            if (type.GetInterface(typeof(IConvertible).Name) == null)
                throw new InvalidOperationException(String.Format("'{0}' cannot be converted as it does not derive from System.IConvertible",
                    type.Name));

            if ((type.IsClass || type.IsInterface) &&
                type != typeof(String))
                return default(T);

            if (type.IsEnum)
                return (T)Enum.Parse(type, value);

            if (type == typeof(String))
                return (T)(object)value;

            if (type == typeof(Boolean))
                return (T)(object)Convert.ToBoolean(value);

            if (type == typeof(Double))
                return (T)(object)Convert.ToDouble(value);

            if (type == typeof(float))
                return (T)(object)Convert.ToDouble(value);

            if (type == typeof(long))
                return (T)(object)Convert.ToInt32(value);

            if (type == typeof(DateTime))
                return (T)(object)Convert.ToDateTime(value);

            // probably not as verbose as it needs to be
            return (T)(object)Convert.ToInt32(value);
        }
    }
}
